﻿Imports BookOrderingSystem.BOSDataSetTableAdapters

Public Class index
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If (Session("UserName") = "") Then
            Response.Redirect("~/Account/Login.aspx")
        End If

    End Sub

    Protected Sub gv_Books_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gv_Books.RowCommand
        If e.CommandName.CompareTo("Add") = 0 Then
            Dim index As Integer = Convert.ToInt32(e.CommandArgument)
            Dim row As GridViewRow = gv_Books.Rows(index)
            Dim bookid As String = gv_Books.Rows(index).Cells(1).Text
            Dim adap As New UsersTableAdapter
            Dim user = adap.GetDataByUserName(Session("UserName"))(0)
            Dim userId = user.UserId
            Dim bookstoreId = ddl_Bookstore.SelectedValue
            Dim count = 0
            Dim cartAdap As New ShoppingCartsTableAdapter
            Dim cartdata = cartAdap.GetDataByBookStoreUser(userId, CInt(bookid), bookstoreId)
            count = cartdata.Count
            Dim quantity As Integer = 0

            If count = 0 Then
                quantity = 1
                cartAdap.Insert(userId, CInt(bookid), bookstoreId, quantity)
            Else
                quantity = cartdata(0).BookQuantity
                quantity += 1
                cartAdap.Update(userId, CInt(bookid), bookstoreId, quantity, userId, CInt(bookid), bookstoreId)
            End If


        End If



    End Sub


    Protected Sub im_shoppingCart_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles im_shoppingCart.Click
        Response.Redirect("~/ShoppingCart/index.aspx")
    End Sub
End Class